<?php
class Cate extends Zend_Db_Table{
  protected $_name = 'cate';
  protected $_primary = 'id';

  public function getCates(){
      return $this->fetchAll(null,'id');
  }

  public function getCateName($cateId){
      $db = $this->getAdapter();
      $where = $db->quoteInto('id = ?', $cateId);
      $res = $this->fetchRow($where);
      if($res!=null) $caterow = $res->toArray();
      else return "未知分类";
      return $caterow['name'];
  }

  public function getCateById($cateId){
      $db = $this->getAdapter();
      $where = $db->quoteInto('id = ?', $cateId);
      $res = $this->fetchRow($where);
      if($res!=null) $res = $res->toArray();
      return $res;
  }

  public function updatecate($id, $set){
      $db = $this->getAdapter();
      $where = $db->quoteInto('id = ?', $id);
      return $this->update($set, $where);
  }

  public function addcate($set){
      return $this->insert($set);
  }

  public function deletecate($id){
      $db = $this->getAdapter();
      $where = $db->quoteInto('id = ?', $id);
      return $this->delete($where);
  }

  public function updateArtCount(){
      $db = $this->getAdapter();

      $cates = $this->fetchAll();
      if($cates == null) return;
      $cates = $cates->toArray();

      foreach ($cates as $cate) {
          $count = $db->fetchOne("SELECT count(*) FROM article WHERE cateid=".$cate['id']);
          $set = Array( 'artcount' => $count, );
          $this->update($set,'id='.$cate['id']);
      }
  }

}
